package com.yonyou.gmmc.service.points.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.marketing.common.constants.CommonConstant;
import org.marketing.common.constants.ResultMsgConstants;
import org.marketing.common.event.BizEvent;
import org.marketing.common.util.CheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.yonyou.gmmc.common.annotation.ParamValid;
import org.marketing.common.Response;
import com.yonyou.gmmc.service.points.constant.PointsConstant;
import com.yonyou.gmmc.service.points.service.TicketService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(value = PointsConstant.BASE_PATH + "/" + PointsConstant.VERSION)
@Api("优惠券相关API")
public class TicketController {
	private Logger logger = Logger.getLogger(TicketController.class);
	@Autowired
	private TicketService ticketService;


    @RequestMapping(value="/checkFans",method = RequestMethod.GET)
    @ApiOperation(value = "校验是否为粉丝", notes = "校验是否为粉丝")
    public Response<Map<String,Object>> checkFans(HttpServletRequest request
    		,@RequestParam(value = "userId_", required = false) String userId_) {
        Map<String,Object> result = new HashMap<>();
        try{
        	String userId="";
    		if(!CheckUtil.NullOrEmpty(userId_)){
    			userId = userId_;
        	}else{
        		userId = request.getHeader(CommonConstant.HEAD_USER_ID);
        	}
            result = ticketService.checkFans(userId);
            return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
        }catch (Exception e) {
            logger.error("查询状态发生异常", e);
            return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
        }
    }
    
    
	/**
	 * 获取优惠券列表
	 * 
	 * @return
	 */
	@RequestMapping(value="/ticketList",method = RequestMethod.GET)
	@ApiOperation(value = "获取优惠券列表", notes = "获取优惠券列表")
	@ParamValid
	public Response<PageInfo<Map<String, Object>>> getTicketList(@RequestParam(required=true,defaultValue="0") Integer pageNum
    		,@RequestParam(required=true,defaultValue="10") Integer pageSize
			,@RequestParam(required=true,defaultValue="0") Integer status
			,@RequestParam(value = "userId_", required = false, defaultValue="") String userId_
			,HttpServletRequest request) {
		logger.info("获取优惠券列表TicketController.getTicketList==>start...");
		try {
        	String userId="";
    		if(!CheckUtil.NullOrEmpty(userId_)){
    			userId = userId_;
        	}else{
        		userId = request.getHeader(CommonConstant.HEAD_USER_ID);
        	}
    		logger.info("获取优惠券列表放入参数");
			Map<String,Object> map = new HashMap<>();
			map.put("pageNum", pageNum);
			map.put("pageSize", pageSize);
			map.put("status", status);
			map.put("userId", userId);
			PageInfo<Map<String, Object>> result = ticketService.getTicketList(map);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, result, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("TicketController.getTicketList==>end...");
		}
	}
	
	/**
	 * 发放优惠券
	 * 
	 * @return
	 */
	@RequestMapping(value="/bindTicket",method = RequestMethod.POST)
	@ApiOperation(value = "发放优惠券", notes = "发放优惠券")
	@ParamValid
	public Response<Map<String,Object>> bindTicket(@RequestBody BizEvent event) {
	    Map<String,Object> resultMap = new HashMap<String,Object>();
		logger.info("TicketController.bindTicket==>start...");
		String bizCode = "1001";
        String triggerCode = "10011001";
        String userId = "10";
        String dealerCode = "";
        String batchId = "";
        String bizId = "";
		try {
		    bizCode = event.getEventData().get("BIZCODE").toString();
		    triggerCode = event.getEventData().get("TRIGGERCODE").toString();
            userId = event.getEventData().get("USERID").toString();
            dealerCode = event.getEventData().get("DEALERCODE").toString();
            batchId = event.getEventData().get("BATCHID").toString();
            bizId = event.getEventData().get("BIZID").toString();
			Map<String,Object> map = new HashMap<String, Object>();
			map.put("bizCode", bizCode);
			map.put("triggerCode", triggerCode);
			map.put("userId", userId);
			map.put("dealerCode", dealerCode);
			//优惠券场景，目前没有
			map.put("batchId", batchId);
			map.put("bizId", bizId);
			List<Long> result = ticketService.bindTicket(map);
			resultMap.put("result", "200");
            resultMap.put("list", result);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, resultMap, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("TicketController.bindTicket==>end...");
		}
	}
	
	/**
	 * 发放优惠券
	 * 
	 * @return
	 */
	@RequestMapping(value="/bindNewUserTicket",method = RequestMethod.GET)
	@ApiOperation(value = "发放优惠给建券后新注册的用户", notes = "发放优惠给建券后新注册的用户")
	public Response<Map<String,Object>> bindNewUserTicket() {
	    Map<String,Object> resultMap = new HashMap<>();
		logger.info("TicketController.bindNewUserTicket==>start...");
		try {
			List<Long> result = ticketService.bindNewUserTicket();
			resultMap.put("result", "200");
            resultMap.put("list", result);
			return new Response<>(ResultMsgConstants.RESULT_CODE_SUCCESS, resultMap, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response<>(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("TicketController.bindNewUserTicket==>end...");
		}
	}
}
